/**
 * 
 */
package com.cms.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.cms.model.Project;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;


public class ProjectServiceImpl extends AbstractDateService implements ProjectService
{
	private Logger logger;
	
	public ProjectServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	@Override
	public Project getProject(int id) 
	{
		String sql = "select * from t_project where id=" + String.valueOf(id) + "";
		ModelGenerator<Project> dbcpmg = new ModelGenerator<Project>(Project.class);
		return dbcpmg.generate(base, sql);
	}

	@Override
	public List<Project> getProjects()
	{
		String sql = "select * from t_project order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<Project> dbcpmg = new ModelGenerator<Project>(Project.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public List<Project> getProjects(int cid) {
		String sql = "select * from t_project where channelid=" + String.valueOf(cid) + " order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<Project> dbcpmg = new ModelGenerator<Project>(Project.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public boolean deleteProject(Project project) {
		String sql = "delete from t_project where id=" + String.valueOf(project.getId()) + "";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean insertProject(Project project) {
		project.setId(getTableId("t_project"));
		String sql = 
			"insert into t_project(id,name,channelid,shipdate,shipdesc,pa,pb,pc,props,status) " +
			"values(" + String.valueOf(project.getId()) + ",'" + project.getName() + "'," + 
					project.getChannelId() + ",'" + project.getShipDate() + "','" +
					project.getShipDesc()+ "','" + project.getPa() + "','" + project.getPb() + "','" +
					project.getPc() + "','" + project.getProps() + "',0)";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean updateProject(Project project) {
		String sql = 
			"update t_project set name='" + project.getName() + "'," +
			"shipdate='" + project.getShipDate() + "'," +
			"shipdesc='" + project.getShipDesc() + "'," +
			"pa='" + project.getPa() + "'," +
			"pb='" + project.getPb() + "'," +
			"pc='" + project.getPc() + "'," +
			"props='" + project.getProps() + "' " +
			" where id=" + project.getId() + "";
		return ConnectionManager.getInstance().execute(base, sql);
	}

}
